package cn.edu.scau.cmi.ema.dao;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.Session;


import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;


import cn.edu.scau.cmi.ema.util.CmiSqlUtil;

@Repository("FillerDAO")
@Transactional
public class FillerDAOImpl extends AbstractJpaDao<Filler> implements FillerDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Filler.class }));

	@PersistenceContext(unitName = "ema")
	private EntityManager entityManager;

	public FillerDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Filler findFillerById(Integer id) throws DataAccessException {

		return findFillerById(id, -1, -1);
	}

	@Transactional
	public Filler findFillerById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findFillerById", startResult, maxRows, id);
			return (Filler) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Filler> findFillerByName(String name) throws DataAccessException {

		return findFillerByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Filler> findFillerByName(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findFillerByName", startResult, maxRows, name);
		return new LinkedHashSet<Filler>(query.getResultList());
	}

	@Transactional
	public Set<Filler> findFillerByNameContaining(String name) throws DataAccessException {

		return findFillerByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Filler> findFillerByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFillerByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Filler>(query.getResultList());
	}

	@Transactional
	public Set<Filler> findFillerByDescription(String description) throws DataAccessException {

		return findFillerByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Filler> findFillerByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFillerByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Filler>(query.getResultList());
	}

	@Transactional
	public Set<Filler> findFillerByDescriptionContaining(String description) throws DataAccessException {

		return findFillerByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Filler> findFillerByDescriptionContaining(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFillerByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Filler>(query.getResultList());
	}

	@Transactional
	public Filler findFillerByPrimaryKey(Integer id) throws DataAccessException {
		return findFillerByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Filler findFillerByPrimaryKey(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findFillerByPrimaryKey", startResult, maxRows, id);
			return (Filler) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Filler> findFillersByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findFillersByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Filler> findAllFillers() throws DataAccessException {

		return findAllFillers(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Filler> findAllFillers(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllFillers", startResult, maxRows);
		return new LinkedHashSet<Filler>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Filler> findFillersByFillertype(Integer fillertypeId, int start, int max) throws DataAccessException {
		Query query = createNamedQuery("findAllFillersByFillertype", start, max, fillertypeId);
		return new LinkedHashSet<Filler>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Filler> findFillerByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Filler>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Filler> findFillersByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Filler.class);
		Session session = entityManager.unwrap(Session.class);
		Set<Filler> fillers = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return fillers;
	}

	@Transactional
	public Filler findFillerByPhysicalPrimaryKey(String name, Integer fillertypeId) throws DataAccessException {
		return findFillerByPhysicalPrimaryKey(name, fillertypeId, -1, -1);
	}

	@Transactional
	public Filler findFillerByPhysicalPrimaryKey(String name, Integer fillertypeId, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findFillerByPhyscialPrimaryKey", startResult, maxRows, name, fillertypeId);
			return (Filler) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Filler entity) {
		return true;
	}
}
